package com.lagou.consumer;

import com.lagou.entity.COrder;
import com.lagou.repository.COrderRepository;
import com.lagou.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Optional;

/**
 * @author mcl
 * @date 2021/10/14 00:04
 * @description
 */
@Slf4j
@Component
@RocketMQMessageListener(topic = "orer_timeout", consumerGroup = "order_timeout_consumer")
public class OrderTimeoutConsumer implements RocketMQListener<COrder> {
    @Autowired
    private COrderRepository cOrderRepository;
    @Autowired
    private ProductService productService;

    @Override
    public void onMessage(COrder order) {
        log.info(order.toString());
        Optional<COrder> byId = cOrderRepository.findById(order.getId());
        if (byId.isPresent()) {
            COrder cOrder = byId.get();
            if ("Paying".equals(cOrder.getStatus())) {

                productService.incrementProductCount(cOrder.getProductId());
                cOrder.setStatus("TimeOut");
                cOrderRepository.save(cOrder);
            }
        } else {
            log.error("订单{}不存在",order.getId());
        }

    }
}
